import java.util.InputMismatchException;
import java.util.Scanner;

public class Game {
    public static void main(String[] args) {
        int[] comNum = getNum();  //机选随机数字
//        for (int item : comNum) {
//            System.out.println(item);
//        }

        //有五次猜的机会
        for (int i = 1; i <= 5; i++) {
            int[] inputNum = input();  //调用输入数字函数
            boolean flag = judge(comNum, inputNum);  //调用判断函数
            if (flag) {  //有4个A，结束循环
                break;
            }
            System.out.println("你还有" + (5 - i) + "次机会");
        }
    }


    //生成随机数
    public static int random(int max, int min) {
        return (int) (Math.random() * (max + 1 - min) + min);
    }

    //机选不重复的4个数
    public static int[] getNum() {
        int[] numarr = new int[4];
        boolean repeat = false;
        for (int i = 0; i < numarr.length; i++) {
            int num = random(9, 0);  //调用随机数函数
            for (int j = 0; j < i; j++) {
                if (num == numarr[j]) {  //判断是否重复
                    repeat = true;
                    break;
                }
            }
            if (repeat) {  //如果重复了
                i--;
            } else {
                numarr[i] = num;
            }
        }
        return numarr;
    }

    //输入数字函数
    public static int[] input() {
        int[] inputNum = new int[4];
        for (int i = 0; i < 4; i++) {
            Scanner scan = new Scanner(System.in);
            try {
                System.out.println("请输入第" + (i + 1) + "个数字:");
                int inNum = scan.nextInt();
                for (int j = 0; j < i; j++) {
                    if (inNum == inputNum[j]) {
                        System.out.println("相同数字，请重新输入");
                        i--;
                    }
                }
                inputNum[i] = inNum;
            } catch (InputMismatchException e) {
                System.out.println("捕获异常");
                i--;
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
        return inputNum;
    }

    //判断函数
    public static boolean judge(int[] arr1, int[] arr2) {
        int count1 = 0;
        int count2 = 0;
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] == arr2[i]) {
                count1++;
            }
            for (int j = 0; j < arr1.length; j++) {
                if (arr1[i] == arr2[j]) {
                    count2++;
                }
            }
        }
        if (count1 == 4) {
            System.out.println("恭喜你，猜对了！");
            return true;
        } else {
            System.out.println(count1 + "个A，" + (count2 - count1) + "个B");
        }
        return false;
    }


}


